package com.jiyinit.web.core.mobile.controller;

import com.jiyinit.core.framework.base.util.DateUtil;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.NoticeRequest;
import com.jiyinit.web.core.business.entity.TabCoreNoticeEntity;
import com.jiyinit.web.core.business.service.NoticeServiceImpl;
import com.jiyinit.web.core.business.vo.NoticeVo;
import com.jiyinit.web.core.common.util.GlobalsUtils;
import com.jiyinit.web.core.mobile.util.Word2PdfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;

/**
 * Created by lr on 2019/1/18.
 */
@Controller
@RequestMapping(value = "mnotice/notice")
public class NnoticeController {

    @Resource(name = "core.noticeServiceImpl")
    private NoticeServiceImpl noticeService;

    private static final Logger logger = LoggerFactory.getLogger(NnoticeController.class);

    /**
     * 通过通知id获取详情，
     * 把信息放到jsp展示，
     */
    @RequestMapping("/preview")
    public String preview(HttpServletRequest request, Integer noticeId) {
        NoticeVo coreNoticeVo = noticeService.selectById(noticeId);
        //没调用一次该方法，查看通知次数+1；
        NoticeRequest noticeRequest=new NoticeRequest();
        noticeRequest.setPreviewNum(coreNoticeVo.getPreviewNum()+1);
        noticeRequest.setNoticeId(noticeId);
        noticeService.updatePreviewNum(noticeRequest);
        if (coreNoticeVo != null) {
            String contents=coreNoticeVo.getNoticeContent();
            if(contents.contains("url=")){
                String[] str=contents.split("url=");
                String content=str[0];
                for(int i=1;i<str.length;i++){
                    String fileUrl=str[i];
                    String serverUrl = getServerUrl(request);
                    String pdfFileName= serverUrl +"/" + GlobalsUtils.getValue("SAVE_WORD_URL")+"/"+fileUrl;
                    request.setAttribute("pdfName"+i,fileUrl);
                    request.setAttribute("url"+i,pdfFileName);
                    logger.info("=======================================pdfFileName{}", pdfFileName);
                }

                String dateTime = DateUtil.dateFormat(coreNoticeVo.getCreateTime(), "yyyy年MM月dd日 HH:mm");
                request.setAttribute("content",content);
                request.setAttribute("dateTime",dateTime);
                request.setAttribute("coreNoticeEntity", coreNoticeVo);
                return "/notice/notice";
            }else{
                request.setAttribute("content",coreNoticeVo.getNoticeContent());
                request.setAttribute("url1","");
                request.setAttribute("url2","");
                request.setAttribute("url3","");
                request.setAttribute("coreNoticeEntity", coreNoticeVo);
                return "/notice/notice";
            }
        }else{
            return null;
        }
    }


    @RequestMapping("/previewPdf")
    public String preview(String url,HttpServletRequest request) {
        logger.info("------------------------------------------url: {}", url);
        request.setAttribute("url",url);
        return "/notice/noticeDetail";
    }

    public String getServerUrl(HttpServletRequest request) {
        String serverHost = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        String serverContext = request.getContextPath().equalsIgnoreCase("/") ? "" : request.getContextPath();
        String basePath = null;
        if (serverHost != null && serverContext != null) {
            basePath = serverHost + serverContext;
        }
        return basePath;
    }
}
